﻿using System;
using System.Text;
using System.Collections;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Linq;

namespace server
{
    class activeWindow
    {
        const int MAXTITLE = 255;
       // private string[] desktopWindowsCaptions;
        private static ArrayList mTitlesList;
        private static IntPtr[] hw_list;

        public activeWindow()
        {
            hw_list = GetDesktopWindowsCaptions();
        }
        public IntPtr[] open_wind
        {
            get { return hw_list; }
        }
        public bool isOk(IntPtr Hwnd)
        {
            return IsWindowVisible(Hwnd);
        }


        private delegate bool EnumDelegate(IntPtr hWnd, int lParam);


        [DllImport("user32.dll", EntryPoint="EnumDesktopWindows",
         ExactSpelling=false, CharSet=CharSet.Auto, SetLastError=true)]
        private static extern bool _EnumDesktopWindows(IntPtr hDesktop,
        EnumDelegate lpEnumCallbackFunction, IntPtr lParam);

        [DllImport("user32.dll", EntryPoint="GetWindowText",
         ExactSpelling=false, CharSet=CharSet.Auto, SetLastError=true)]
        private static extern int _GetWindowText(IntPtr hWnd,
        StringBuilder lpWindowText, int nMaxCount);
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool IsWindowVisible(IntPtr hWnd);


        private static bool EnumWindowsProc(IntPtr hWnd, int lParam)
        {
            mTitlesList.Add(hWnd);
            return true;
        }

        public string getText(IntPtr hwnd)
        {
            return GetWindowText(hwnd);
        }
        /// <summary>
        /// Returns the caption of a windows by given HWND identifier.
        /// </summary>
        public static string GetWindowText(IntPtr hWnd)
        {
            StringBuilder title = new StringBuilder(MAXTITLE);
            int titleLength = _GetWindowText(hWnd, title, title.Capacity + 1);
            title.Length = titleLength;

            return title.ToString();
        }

        /// <summary>
        /// Returns the caption of all desktop windows.
        /// </summary>
        public static IntPtr[] GetDesktopWindowsCaptions()
        {
            mTitlesList = new ArrayList();
            EnumDelegate enumfunc = new EnumDelegate(EnumWindowsProc);
            IntPtr hDesktop = IntPtr.Zero; // current desktop
            bool success = _EnumDesktopWindows(hDesktop, enumfunc, IntPtr.Zero);

            if (success)
            {
                // Copy the result to string array
                IntPtr[] titles = new IntPtr[mTitlesList.Count];
                mTitlesList.CopyTo(titles);
                return titles;
            }
            else
            {
                // Get the last Win32 error code
                int errorCode = Marshal.GetLastWin32Error();

                string errorMessage = String.Format("EnumDesktopWindows failed with code {0}.", errorCode);
                throw new Exception(errorMessage);
            }
        }
    }
}
